﻿using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using GSQ.BusinessEntities;
using GSQ.DataAccess.DataSetTableAdapters;

namespace GSQ.DataAccess
{
    public class DepartamentosDA
    {
        private DEPARTAMENTOSTableAdapter Adapter = new DEPARTAMENTOSTableAdapter();
        private DataSet.DEPARTAMENTOSDataTable entities;
        private DataSet.DEPARTAMENTOSRow entity;

        public decimal Insert(DEPARTAMENTOS value)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    Adapter.Connection = cnn;

                    entities = new DataSet.DEPARTAMENTOSDataTable();

                    entity = entities.NewDEPARTAMENTOSRow();

                    entity.ID = value.ID;
                    entity.DEPARTAMENTO = value.DEPARTAMENTO.Trim();
                    entity.ACTIVO = value.ACTIVO;

                    entities.AddDEPARTAMENTOSRow(entity);
                    Adapter.Update(entities);

                    value.ID = entity.ID;
                    return value.ID;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public decimal Update(DEPARTAMENTOS value)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.Adapter.Connection = cnn;

                    entities = Adapter.GetDataByID(value.ID);
                    entity = entities.FirstOrDefault();


                    entity.DEPARTAMENTO = value.DEPARTAMENTO.Trim();
                    entity.ACTIVO = value.ACTIVO;

                    this.Adapter.Update(entities);

                    return entity.ID;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public DEPARTAMENTOS GetById(decimal id)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.Adapter.Connection = cnn;

                    DEPARTAMENTOS banco;

                    entity = this.Adapter.GetDataByID(id).FirstOrDefault();

                    if (entity != null)
                    {
                        banco = new DEPARTAMENTOS
                            {
                                ID = entity.ID,
                                DEPARTAMENTO = entity.DEPARTAMENTO,
                                ACTIVO = entity.ACTIVO
                            };
                    }
                    else
                        banco = null;

                    return banco;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public List<DEPARTAMENTOS> GetAll()
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.Adapter.Connection = cnn;

                    List<DEPARTAMENTOS> lst = new List<DEPARTAMENTOS>();
                    foreach (DataSet.DEPARTAMENTOSRow rw in this.Adapter.GetData())
                    {
                        lst.Add(new DEPARTAMENTOS
                        {
                            ID = rw.ID,
                            DEPARTAMENTO = rw.DEPARTAMENTO,
                            ACTIVO = rw.ACTIVO
                        });
                    }
                    cnn.Close();
                    return lst;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public List<DEPARTAMENTOS> GetByDescripcion(string value)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.Adapter.Connection = cnn;

                    List<DEPARTAMENTOS> lst = new List<DEPARTAMENTOS>();
                    foreach (DataSet.DEPARTAMENTOSRow rw in this.Adapter.GetDataByDepartamento(value.Trim()))
                    {
                        lst.Add(new DEPARTAMENTOS
                        {
                            ID = rw.ID,
                            DEPARTAMENTO = rw.DEPARTAMENTO,
                            ACTIVO = rw.ACTIVO
                        });
                    }
                    return lst;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }
    }
}
